<?php
class AlbumController extends AppController {

	public $name = 'album';

	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','getalbumcategoryadd','addpost','delimage','upalbumcat','delabum');

	}
	
	public function upalbumcat() {
        $id=intval(Request::get_args("id"));
		$page = intval(Request::get_args("page"));
		$cat = intval(Request::get_args("cat"));
		$newcat = intval(Request::get_args("newcat"));		
        $this->loadModel('imgsize');  
        $this->loadModel('shopalbumcategory');
		
		$query = array(
			'conditions' => array('OR'=>array(array('shopalbumcategory.id' => $cat),array('shopalbumcategory.id' => $newcat)),'AND'=>array(array('shopalbumcategory.shop_id' => $this->my->id))), 
			'fields' => array('shopalbumcategory.*'), 
		);
        $shop_category = POP::exe('[@@:UM shopalbumcategory][@@:QU find:all]',$query);		
		
		if( isset($shop_category[0]['shopalbumcategory']['id']) && isset($shop_category[1]['shopalbumcategory']['id']) && $shop_category[0]['shopalbumcategory']['id']!=$shop_category[1]['shopalbumcategory']['id'] ) {
            if( $id && $cat && $newcat && $cat != $newcat ) {//移动单个图片到文件夹
				$query = array(
					'conditions' => array('imgsize.id' => $id,'imgsize.shopalbum_id' => $cat,'imgsize.uid' => $this->my->id), 
					'fields' => array('imgsize.*'), 
				);
				$row = POP::exe('[@@:UM imgsize][@@:QU find:first]',$query);				
				
				
                if($row){				
					$query  = array();		
					$query['keyid'] = true;
					$safeData['imgsize'] = array('shopalbum_id'=>$newcat);				
					$query['set'] = $safeData;	
					$query['conditions'] = array('imgsize.uid'=>$this->my->id,'imgsize.id'=>$id);		
                    if(POP::exe('[@@:UM imgsize][@@:QU save]',$query)) {
                        $this->flash('操作成功!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
                    } else {
                        $this->flash('操作失败!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
                    }
                } else {
                    $this->flash('该图片不存在!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
                }			
            } else if( empty($id) && $cat && $newcat && $cat != $newcat ) {//合并文件夹
				$query  = array();		
				$query['keyid'] = true;
				$safeData['imgsize'] = array('shopalbum_id'=>$newcat);				
				$query['set'] = $safeData;	
				$query['conditions'] = array('imgsize.uid'=>$this->my->id,'imgsize.shopalbum_id'=>$cat);		
				if(POP::exe('[@@:UM imgsize][@@:QU save]',$query)) {			
                    $this->flash('操作成功!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
                } else {
                    $this->flash('操作失败!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
                }
            } else if ( $cat && $newcat && $cat == $newcat ) {//同个文件夹不能移动
                $this->flash('自己不能移到自己的相册中去!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
            } else {
		        $this->flash('操作失败', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
            }
		} else if( isset($shop_category[0]['shopalbumcategory']['id']) && !isset($shop_category[1]['shopalbumcategory']['id'])) {	
            $this->flash('自己不能移到自己的相册中去!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
		} else {
            $this->flash('相册不存在', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
        }		
    }
	
	public function delabum() {	
		$page = intval(Request::get_args("page"));
		$cat = intval(Request::get_args("cat"));
		if($cat) {
			$query = array(
				'conditions' => array('imgsize.shopalbum_id' => $cat,'imgsize.uid' => $this->my->id), 
				'fields' => array('imgsize.*'), 
			);
			$row = POP::exe('[@@:UM imgsize][@@:QU find:first]',$query);		
		
		    if (empty($row)) {
				$query = array('keyid'=>$cat);			
			    if(POP::exe('[@@:UM shopalbumcategory][@@:QU del]',$query)) {
			        $this->flash('删除成功!', Router::url('shop/album/'.$this->my->id.'/?page='.$page));
			    }else {
			        $this->flash('删除失败!', Router::url('shop/album/'.$this->my->id.'/?page='.$page));
			    }
		    } else {
		        $this->flash('该相册当中还有图片，请先删除图片后再删除图片!', Router::url('shop/album/'.$this->my->id.'/?page='.$page));
		    }
		}else {
		    $this->flash('非法操作!', Router::url('shop/album/'.$this->my->id.'/?page='.$page));
		}
	}
	
	public function delimage() {	
        $id=intval(Request::get_args("id"));
		$page = intval(Request::get_args("page"));
		$cat = intval(Request::get_args("cat"));		
        $update =0;
        $this->loadModel('imgsize'); 
		$query = array(
			'conditions' => array('imgsize.id' => $id,'imgsize.uid' => $this->my->id), 
			'fields' => array('imgsize.*'), 
		);
		$row = POP::exe('[@@:UM imgsize][@@:QU find:first]',$query);
	
        if($row){
	        $size = $row['imgsize']['img_size'];
	        $name = $row['imgsize']['img_url'];
	        $path= $row['imgsize']['img_url'];
			$query = array(
				'conditions' => array('goodsgallery.img_size_id' => $id), 
				'fields' => array('goodsgallery.*'), 
			);
			$rs = POP::exe('[@@:UM goodsgallery][@@:QU find:first]',$query);			
	
	        //根据$t_img_size 的id查询出$t_goods_gallery表中的数据
	        if($rs){	
		        //删除$t_img_size和$t_goods_gallery表中的数据
		        $img_id = $rs['goodsgallery']['id'];
				$query = array('keyid'=>$img_id);			
				POP::exe('[@@:UM goodsgallery][@@:QU del]',$query);		
		        //删除图片
		        @unlink(rtrim(Param::read('wwwrootPatch'),'/') . str_replace($this->base,'',$rs['goodsgallery']['img_url']));
		        @unlink(rtrim(Param::read('wwwrootPatch'),'/') . str_replace($this->base,'',$rs['goodsgallery']['thumb_url']));
		        @unlink(rtrim(Param::read('wwwrootPatch'),'/') . str_replace($this->base,'',$rs['goodsgallery']['img_original']));
		
	        }
	        //然后更新将shop_info表中的总图片大小更新
			$query  = array();		
			$query['keyid'] = true;
			$safeData['shopinfo'] = array('count_imgsize -'=>$size);				
			$query['set'] = $safeData;	
			$query['conditions'] = array('shopinfo.user_id'=>$this->my->id);		
			POP::exe('[@@:UM shopinfo][@@:QU save]',$query);			

	        //删除$t_img_size表中的数据
			$query = array('keyid'=>$id);			
			$update = POP::exe('[@@:UM imgsize][@@:QU del]',$query);			
	        $name = rtrim(Param::read('wwwrootPatch'),'/') . str_replace($this->base,'',$path);	 
	        if($update && file_exists($name)){
		        @unlink($name); 
	        }
	        if($update){
		        $this->flash('删除成功!', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
	        }else{
		        $this->flash('删除失败', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
	        }
        }else{
	        $this->flash('删除失败', Router::url('shop/albumshow/'.$this->my->id.'/?page='.$page.'&cat='.$cat));
        }	
	}	
	
	
	
	public function addpost() {	
	    $shopalbum_id = intval(Request::$data["ucat_id"]);
		$image_size_id = Request::$data["image_size_id"];
		if($shopalbum_id>0) {
		    if(!empty($image_size_id)) {
				$query  = array();		
				$query['keyid'] = true;
				$safeData['imgsize'] = array('shopalbum_id'=>$shopalbum_id);				
				$query['set'] = $safeData;	
				$query['conditions'] = array('imgsize.id'=>$image_size_id,'imgsize.uid'=>$this->my->id);		
                if(POP::exe('[@@:UM imgsize][@@:QU save]',$query)) {
                    $this->flash('提交成功!', Router::url('album/index'));
                } else {
                    $this->flash('提交失败!', Router::url('album/index'));
                }				
            } else {
                $this->flash('请上传照片！', Router::url('album/index'));
            }		
		} else {
		    $this->flash('请选择上传相册！', Router::url('album/index'));
		}   
	}
	
	public function index() {
        $this->_teamRights();	
	    $shop_id = $this->my->id;
		
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.*'), 
		);
		$SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);

        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($shop_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		
        if($shop_id > 0){
            if($this->my->locked==1){
	            $this->C_session->destroy();
	            exit("您已经被锁定了");//非法操作
            }
        }
		$query = array(
			'conditions' => array('userrank.id' => $this->my->rank_id), 
			'fields' => array('userrank.*'), 
		);
		$info = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);		
	
        $privilege = unserialize($info['userrank']['privilege']);		
		if(isset($privilege[8])) {
		    $Mcount = $privilege[8];
		}else {
		    $Mcount = Param::read('upfiles.usersizelimit');		
		}
		
		$query = array(
			'conditions' => array('shopalbumcategory.shop_id' => $shop_id), 
			'fields' => array('shopalbumcategory.*'), 
			'order' => 'sort_order asc',
		);
		$shop_category = POP::exe('[@@:UM shopalbumcategory][@@:QU find:all]',$query);		

        $html_shop_category = $this->html_format_shop_category($shop_category,0);		
        $this->set('html_shop_category',$html_shop_category);	
	    $this->set('ps',$this->C_session->id());
	    $this->set('SHOP',$SHOP);
	    $this->set('Mcount',$Mcount);		
	}
	
	public function html_format_shop_category($array,$value,$parentid=0,$level=0) {
	    $str = '';
	    $str_pad = $this->getnbsp($level);
	    foreach($array as $v) {
		    $selected = '';
		    if($v['shopalbumcategory']['parent_id'] == $parentid) {
			    if($value == $v['shopalbumcategory']['id']) { $selected = 'selected';}
			    $str .= "<option value='".$v['shopalbumcategory']['id']."' ".$selected.">".$str_pad.$v['shopalbumcategory']['album_cat_name']."</option>";
			    $str .= $this->html_format_shop_category($array,$value,$v['shopalbumcategory']['id'],($level+1));
		    }
	    }
	    return $str;
    }
	
	public function getnbsp($i) {
	    $str = '';
	    if($i) {
		    for($j=0; $j<$i; $j++) {
			    $str .= "　";
		    }
	    }
	    return $str;
    }	
	
	public function getalbumcategoryadd() {	
        $this->_teamRights();	
        $post['shopalbumcategory']['album_cat_name'] = iconv('UTF-8','GB2312' , $_POST['name']);
        $post['shopalbumcategory']['parent_id'] = 0;
        $post['shopalbumcategory']['album_unit'] = '';
        $post['shopalbumcategory']['sort_order'] = '0';
        $post['shopalbumcategory']['shop_id'] = $this->my->id;
        if(empty($post['shopalbumcategory']['album_cat_name'])) {
	        exit("-1");
        }	
		$getid = POP::exe('[@@:UM shopalbumcategory][@@:QU save]',$post);				
        if($getid) {
	        echo $getid;
	        exit();
        } else {
	        exit("-1");
        }		
	}		
	
}
?>